//1、创建管理者（构造函数）
function SquareFactory() {

}

//这个方法是用来初始化小方块的
SquareFactory.prototype.init = function (square, color, action) {
    square.viewContent.style.position = 'absolute';
    square.viewContent.style.width = square.width + 'px';
    square.viewContent.style.height = square.height + 'px';
    square.viewContent.style.backgroundColor = color;

    //x代表列；y代表行；
    square.viewContent.style.left = square.x * square.width + 'px';
    square.viewContent.style.top = square.y * square.height + 'px';

    //给每一个方块实例身上添加一条属性，这条属性代表这个方块的类型
    square.collide=action;
}

//2、包装创建方块的构造函数

//创建地板的构造函数
SquareFactory.prototype.Floor = function (x, y, color) {
    var floor = new Floor(x, y, squareWidth, squareWidth);
    this.init(floor, color, collideType.move);
    return floor;
};
//创建围墙的构造函数
SquareFactory.prototype.Wall = function (x, y, color) {
    var wall = new Wall(x, y, squareWidth, squareWidth);
    this.init(wall, color, collideType.die);
    return wall;
};
//创建蛇头的构造函数
SquareFactory.prototype.SnakeHead = function (x, y, color) {
    var snakeHead = new SnakeHead(x, y, squareWidth, squareWidth);
    this.init(snakeHead, color, collideType.die);
    snakeHead.upData(x,y);

    return snakeHead;
};
//创建蛇身的构造函数
SquareFactory.prototype.SnakeBody = function (x, y, color) {
    var snakeBody = new SnakeBody(x, y, squareWidth, squareWidth);
    this.init(snakeBody, color, collideType.die);
    return snakeBody;
};
//创建蛇身的构造函数
SquareFactory.prototype.Food = function (x, y, color) {
    var food = new Food(x, y, squareWidth, squareWidth);
    this.init(food, color, collideType.eat);
    food.upData(x,y);
    return food;
};


//3、提供一个对外创建方块的接口
SquareFactory.create = function (type, x, y, color) {
    if (SquareFactory.prototype[type] == 'undefined') {
        throw 'no this type';
    }

    SquareFactory.prototype[type].prototype = new SquareFactory();

    return new SquareFactory.prototype[type](x, y, color);
};

// var a=SquareFactory.create('Wall',1,1,'black');

